package com.bawei.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.domain.query.DocQueryReq;
import com.bawei.domain.request.DocInfoReq;
import com.bawei.domain.request.DocUpdReq;
import com.bawei.es.service.EsDocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/es/doc")
public class EsDocController {

    @Autowired
    private EsDocService esDocService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 创建索引
     * @param docInfoReq
     * @return
     */
    @PostMapping("/createDoc")
    public R createDoc(@RequestBody DocInfoReq docInfoReq){
        log.info("功能名称:Es文档的创建,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(docInfoReq));
        R doc = esDocService.createDoc(docInfoReq);
        log.info("功能名称:Es文档的创建,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(doc));
        return doc;
    }


    /**
     * 删除文档
     * @param id
     * @param indexName
     * @return
     */
    @PostMapping("/deleteDoc")
    public  R deleteDoc(@RequestParam String id,@RequestParam String indexName){
        log.info("功能名称:Es文档的删除,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(indexName));
        esDocService.deleteDoc(id,indexName);
        return R.ok();
    }


    /**
     * 查询文档
     */
    @PostMapping("/queryDoc")
    public  R queryDoc(@RequestBody DocQueryReq docQueryReq){
        log.info("功能名称:Es文档的查询,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(docQueryReq));
        Object o = esDocService.queryIndex(docQueryReq);
        log.info("功能名称:Es文档的查询,请求URI:{},请求方式:{}.响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(docQueryReq));
        return R.ok(o);
    }


    /**
     * 修改文档
     */

    @PostMapping("/updateDoc")
    public  R updateDpc(@RequestBody DocUpdReq docUpdReq){
        log.info("功能名称:Es文档的修改,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(docUpdReq));
        esDocService.updateDoc(docUpdReq);
        return R.ok();
    }







}
